package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.graphics.BitmapFactory
import android.graphics.Rect
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.model.param.EditBloodHoseParam
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityEditInfoStyleBinding
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat


/**
 * @Author      : hxw
 * @Date        : 2024/9/21 15:39
 * @Describe    : 选择风格样式（编辑信息）
 */
class ActivityEditInfoStyle : BaseActivity<BreedingPigeonViewModel, ActivityEditInfoStyleBinding>() {

    private var mId = 0
    private var editId = 0
    private var mImagePath = ""

    private var mHouseInfoStatus = 1
    private var mHouseNameStatus = 1
    private var mHouseAddressStatus = 1
    private var mHouseMobileStatus = 1
    private var mHouseEmailStatus = 1
    private var mHouseKWaiStatus = 1
    private var mHouseTikTokStatus = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 获取屏幕高度
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        val screenHeight = displayMetrics.heightPixels
        // 监听软键盘状态
        mDatabind.llRootLayout.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            mDatabind.llRootLayout.getWindowVisibleDisplayFrame(r)
            /** 计算高度差 */
            val heightDiff = mDatabind.llRootLayout.height - (r.bottom - r.top)
            if (heightDiff > screenHeight / 4) {
                // 软键盘弹出，界面上移
                mDatabind.llRootLayout.scrollTo(0, heightDiff + 80)
            } else {
                // 软键盘收起，界面下移
                mDatabind.llRootLayout.scrollTo(0, 0)
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "选择风格样式"

        val result = intent.getStringExtra("bitmap").toString()
        val uri = Uri.parse(result)
        val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri))
        // 使用 Bitmap，例如显示在 ImageView
        mDatabind.ivBloodBookImage.setImageBitmap(bitmap)
        mId = intent.getIntExtra("id", 0)

        showLoading(getString(R.string.loading))
        mViewModel.getBloodBookHouse(mId.toString())

        initSwitchClickListener()
    }

    private fun initSwitchClickListener() {
        mDatabind.switchHouseInfo.setOnCheckedChangeListener { _, isChecked ->
            mHouseInfoStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
        mDatabind.switchHouseName.setOnCheckedChangeListener { _, isChecked ->
            mHouseNameStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
        mDatabind.switchHouseAddress.setOnCheckedChangeListener { _, isChecked ->
            mHouseAddressStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
        mDatabind.switchHouseEmail.setOnCheckedChangeListener { _, isChecked ->
            mHouseEmailStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
        mDatabind.switchHousePhone.setOnCheckedChangeListener { _, isChecked ->
            mHouseMobileStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
        mDatabind.switchHouseKWai.setOnCheckedChangeListener { _, isChecked ->
            mHouseKWaiStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
        mDatabind.switchHouseKTikTok.setOnCheckedChangeListener { _, isChecked ->
            mHouseTikTokStatus = if (isChecked) {
                1
            } else {
                2
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        /** 修改鸽舍信息 */
        mViewModel.editHouseResult.observe(this) {
            eventViewModel.toRefreshData.value = true
            finish()
        }
        /** 获取鸽舍信息 */
        mViewModel.getBloodBookHouseResult.observe(this) {
            editId = it.id
            mDatabind.data = it
            mHouseInfoStatus = it.infoStatus
            mHouseNameStatus = it.nameStatus
            mHouseAddressStatus = it.addressStatus
            mHouseEmailStatus = it.emailStatus
            mHouseMobileStatus = it.mobileStatus
            mHouseKWaiStatus = it.kWaiStatus
            mHouseTikTokStatus = it.tikTokStatus
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.btnSavePigeonHouseInfo) {
            when (it) {
                mDatabind.btnSavePigeonHouseInfo -> editBloodBookInfo()
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun editBloodBookInfo() {
        val mAddress = mDatabind.etPigeonAddress.text.toString().trim()
        val mEmail = mDatabind.etPigeonEmail.text.toString().trim()
        val mKWai = mDatabind.etPigeonKWai.text.toString().trim()
        val mMobile = mDatabind.etPigeonPhone.text.toString().trim()
        val mName = mDatabind.etPigeonHouse.text.toString().trim()
        val mTikTok = mDatabind.etPigeonKTikTok.text.toString().trim()
        val mHouseInfo = mDatabind.etPigeonHouseInfo.text.toString().trim()
        val param = EditBloodHoseParam(
            address = mAddress,
            bloodBookId = mId,
            email = mEmail,
            id = editId,
            imagePath = mImagePath,
            info = mHouseInfo,
            kWai = mKWai,
            mobile = mMobile,
            name = mName,
            tikTok = mTikTok,
            infoStatus = mHouseInfoStatus,
            nameStatus = mHouseNameStatus,
            addressStatus = mHouseAddressStatus,
            mobileStatus = mHouseMobileStatus,
            emailStatus = mHouseEmailStatus,
            kWaiStatus = mHouseKWaiStatus,
            tikTokStatus = mHouseTikTokStatus,
        )
        showLoading(getString(R.string.loading))
        mViewModel.editBloodBookHouse(param)
    }
}